#======================================================================
# Product:      KnowledgeEngine (Plone Version)
#
# File:         AtomAPI.py
#
# Copyright:    Copyright (c) 2006, Faulkner Technologies
#
# Author:       John Meredith
#
# Description:  AtomAPI blog client
#======================================================================


#======================================================================
# KnowledgeEngine Imports
#----------------------------------------------------------------------
from AbstractBlogClient import *


#======================================================================
# Zope Library Imports
#----------------------------------------------------------------------


#======================================================================
# Python Library Imports
#----------------------------------------------------------------------
import base64
import binascii
from httplib import HTTPConnection
import urllib
import urlparse


#======================================================================
# Class definition
#----------------------------------------------------------------------
class AtomAPI(AbstractBlogClient):
    """
        An AtomAPI client
    """


    # Some meta-data for the blog client
    TITLE = "AtomAPI (Generic)"

    def getServer(self):
        """
            Returns a HTTP connection
        """
        
        # Return a connection to the host part of serverURL
        return HTTPConnection(self.parseServerURL()[1])


    def parseServerURL(self):
        """
            Delegate method for urlparse.urlparse on the serverURL
            
            Returns: (scheme, netloc, path, params, query, fragment) tuple
        """
        
        return urlparse.urlparse(self.serverURL)


    def getHeaders(self):
        """
            Generates the HTTP connection headers
        """
        
        # Generate our basic authentication key
        cookie = binascii.b2a_base64("%s:%s" % (self.username, self.password))
        headers = {
            "Content-type": 'application/atom+xml',
            "Authorization": 'BASIC %s' % cookie.strip(),
            "UserAgent": 'pyatomblogger',
        }
        
        return headers


    def isValidConnection(self):
        """
            Check we have a valid connection by authenticating against
            the URL
        """

        server = self.getServer()
        server.request("POST", self.parseServerURL()[2], None, self.getHeaders())
        
        response = server.getresponse()
        
        print "Response: %s" % (response.status, )

        exit


    def getCategories(self):
        """
            Return a list of available categories
        """
        
        return ()


    def getBlogs(self):
        """
            Return a list of available blogs
        """

        return ()
        

    def createPost(self, **args):
        """
            Create a new post.
        """
        
        return False


#======================================================================
# Register Blog client with the action
#----------------------------------------------------------------------
from Products.KnowledgeEngine.Actions.BlogPost.Clients import registerBlogClient
registerBlogClient(AtomAPI)
